package com.edu.springboot.controller;

import com.edu.springboot.bean.Books;
import com.edu.springboot.bean.User;
import com.edu.springboot.bean.UserLogin;
import com.edu.springboot.service.BookService;
import com.edu.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
public class MainController {

    @Autowired
    private UserService userService;

    @Autowired
    private BookService bookService;
//  主页面
    @RequestMapping("/")
    public String login() throws Exception
    {
        return "login";
    }

//    登录界面
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String toMain(HttpServletRequest request, Model model, HttpSession session) throws Exception {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 判断用户是否登录成功
        if (userService.selectlogin(username, password) != null) {
            session.setAttribute("loginUser", new UserLogin(username, password));
            return "index";
        } else {
            model.addAttribute("message", "遗憾，用户登录失败~");
            return "login";
        }

//        List<UserLogin> userList = userService.selectlogin();
//        for (UserLogin user : userList) {
//            if (user.getUsername() != null && user.getPassword() != null && user.getUsername().equals(username) && user.getPassword().equals(password)) {
//                session.setAttribute("loginUser", new User(username, password));
//                return "index";
//            } else {
//                modelMap.put("message", "你的用户名密码不对");
//                return "login";
//            }
//
//        }
//        return "login";
    }

//    基本表格显示
    @RequestMapping("/basicTable")
    public String toBasicTable(ModelMap modelMap) throws Exception
    {

        List<UserLogin> logins = userService.selectUser();
        modelMap.put("users",logins);

        List<User> userr = userService.selectUserr();
        modelMap.put("userss",userr);

        return "table/basic_table";
    }

//    没有登录效果
    @RequestMapping("/main")
    public String toMain(HttpSession session,ModelMap modelMap) throws Exception
    {
        final Object loginUser = session.getAttribute("loginUser");
        //表示没有登录
        if (loginUser == null) {
            modelMap.put("message", "你还没有登录，请先再操作");
            return "login";
        }
        return "index";

    }

//    高级表效果
    @RequestMapping("/dynamicTable")
    public String toDynamicTable(ModelMap modelMap) throws Exception
    {

        List<Books> books = bookService.selectAll();
        modelMap.put("books",books);

        return "table/dynamic_table";
    }

//  跳转编辑表页面
    @RequestMapping("/editableTable")
    public String toEditableTable() throws Exception
    {
        return "table/editable_table";
    }

    @RequestMapping("/increasebooks")
    public String selecrAl() throws Exception{

        return "table/increase_books";
    }

}

